package adventure.commands.admin;

import adventure.*;
import adventure.commands.*;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class AdminPromote extends AbstractCommand {

    public AdminPromote(Adventure a) {
        super(a, new String[]{"ADMINPROMOTE"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (p.isAdmin()) {
            if (args.length == 2) {
                return promoteToAdmin(p, args[1]);
            } else {
                return "Promote who to admin?\nSyntax: adminpromote <player name>";
            }
        } else {
            return "Command not recognised.";
        }
    }

    private String promoteToAdmin(Player p, String target) {
        Player targetPlayer = players.getOnlinePlayer(target);

        if (targetPlayer.getID() == p.getID()) {
            return "You are already an administrator!";
        }

        if (targetPlayer.getID() > 0) {
            if (targetPlayer.isSuperAdmin()) {
                return ColourUtil.colourise(targetPlayer.getAliveName(), ColourUtil.player) + " is already a super administrator!";
            } else if (targetPlayer.isAdmin()) {
                return ColourUtil.colourise(targetPlayer.getAliveName(), ColourUtil.player) + " is already an administrator!";
            }


            ((PlayerEdit) targetPlayer).setAdmin(true);
            players.savePlayers();
            out.broadcastToPlayer(targetPlayer,
                    ColourUtil.colourise(p.getAliveName(), ColourUtil.player)
                    + ColourUtil.colourise(" has promoted you to administrator! Type 'help admin' for a description of the administrator commands at your disposal.", ColourUtil.white));

            /*out.broadcastServerMessage(ColourUtil.colourise(p.getAliveName(), ColourUtil.player)
             + ColourUtil.colourise(" has promoted ", ColourUtil.white)
             + ColourUtil.colourise(targetPlayer.getAliveName(), ColourUtil.player) 
             + ColourUtil.colourise(" to administrator!", ColourUtil.white));*/

            return "You have promoted "
                    + ColourUtil.colourise(targetPlayer.getAliveName(), ColourUtil.player)
                    + " to administrator!";
        } else {
            targetPlayer = players.getOfflinePlayer(target);

            if (targetPlayer.getID() > 0) {
                return "Player " + ColourUtil.colourise(target, ColourUtil.player)
                        + " must be online to promote to administrator!";
            } else {
                return "Player " + target + " does not exist!";
            }
        }
    }
}
